package com.blog.controller;

import cn.hutool.captcha.LineCaptcha;
import com.blog.dto.*;
import com.blog.entity.Users;
import com.blog.service.UsersService;
import com.blog.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/public/user")
public class LoginController {
    private final UsersService usersService;

    @Resource
    private RedisUtils redisUtils;
    public LoginController(UsersService usersService) {
        this.usersService = usersService;
    }
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO users) {
        return usersService.login(users);
    }

    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        return usersService.register(registerDTO);
    }

    @PostMapping("/captcha_picture")
    public Result captchapicture() {
        log.info("captcha_picture");
        return usersService.captchapicture();
    }

    @PostMapping("/emailcode")
    public Result email(@RequestBody EmailDTO emaildto) {
        return usersService.setemailcode(emaildto);
    }

    @PostMapping("/refresh")
    public Result refresh(@RequestBody String oldCaptchaId) {
        if(redisUtils.get(oldCaptchaId) != null) {
            redisUtils.delete(oldCaptchaId);
        }
        return usersService.captchapicture();
    }

    @PostMapping("/reset_password")
    public Result reset_password(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        return usersService.reset_password(resetPasswordDTO);
    }

    @PostMapping("/refreshtoken")
    public Result refreshToken(@RequestBody Map<String, String> refreshTokendto) {
        String refreshToken = refreshTokendto.get("refreshToken");
        return usersService.refreshToken(refreshToken);
    }
    @GetMapping("/userinfo")
    public Result getUserInfo(@RequestHeader("Authorization") String token) {
        return usersService.getUserInfo(token);
    }
}
